const express = require('express')//express框架
const { Op } = require('sequelize')//nodejs的sequelize模块
const room = require('../crud/table/room.js')//引入放假信息表
const roomType = require('../crud/table/roomType.js')//引入所有房间类型表

room.hasOne(roomType,{ sourceKey:'type',foreignKey:'type'})//每个房间都有一个房间类型

const roomRouter = express.Router()

roomRouter.post('/add',(req,res) =>{//添加房间
    const { roomNumber, type, roomStatus, remarks } = req.body;
    room.findOne({
        where:{
            roomNumber
        }
    })
    .then(data =>{
        if(data){
            res.status(200).json({
                success:false,
                msg:'该房间已经存在！'
            })
            return new Promise(() =>{})
        }else{
            return room.create({
                roomNumber, type, roomStatus, remarks
            })
        }
    })
    .then(data =>{
        return room.findOne({
            where:{
                roomNumber
            },
            include:[{
                model:roomType,
                foreignKey:'type',
                attributes:['price','url']
            }]
        })
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'房间添加成功！',
            room:data.get()
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

roomRouter.delete('/del',(req,res) =>{
    const { roomNumber } = req.body;
    room.findOne({
        where:{
            roomNumber
        }
    })
    .then(data =>{
        if(data){
            return room.destroy({
                where:{
                    roomNumber
                }
            })
        }else{
            res.status(200).json({
                success:false,
                msg:'房间删除失败！'
            })
            return new Promise(() =>{})
        }
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'该房间删除成功！'
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

roomRouter.put('/update',(req,res) =>{//修改房间信息
    const { roomNumber, type, roomStatus,remarks } = req.body;
    room.findOne({
        where:{
            roomNumber
        }
    })
    .then(data =>{
        if(data){
            return room.update({
                type, roomStatus,remarks
            },{
                where:{
                    roomNumber
                }
            })
        }else{
            res.status(200).json({
                success:false,
                msg:'该房间不存在！'
            })
            return new Promise(() =>{})
        }
    })
    .then(data =>{
        return room.findOne({
            where:{
                roomNumber
            }
        })
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'房间信息修改成功！',
            room:data.get()
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误'
        })
    })
})

roomRouter.get('/getAllRoom',(req,res) =>{//获取所有房间信息
    const { pageSize, offset } = req.query;
    room.findAndCountAll({
        limit:pageSize,
        offset:offset
    })
    .then(data =>{
        let roomList = data.rows.map(item =>{
            return item.get()
        })
        res.status(200).json({
            success:true,
            msg:'房间信息查询成功！',
            roomList:roomList,
            count:data.count
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

roomRouter.get('/getOneRoom',(req,res) =>{//获取某个房间号的房间信息
    const { roomNumber } = req.query;
    room.findOne({
        where:{
            roomNumber
        }
    })
    .then(data =>{
        if(data){
            res.status(200).json({
                success:true,
                msg:'房间查询成功！',
                room:data.get()
            })
        }else{
            res.status(200).json({
                success:false,
                msg:'未查询到该房间信息'
            })
        }
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

roomRouter.get('/getNotInRoom',(req,res) =>{//获取未入住房间信息
    const { pageSize, offset } = req.query;
    room.findAndCountAll({
        limit:pageSize,
        offset:offset,
        where:{
            roomStatus:{
                [Op.eq]:'未入住'
            }
        }
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'未入住房间查询成功!',
            roomList:data.rows.map(item =>{
                return item.get()
            }),
            count:data.count
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

roomRouter.get('/getInRoom',(req,res) =>{//查询已入住房间信息
    const { pageSize, offset } = req.query;
    room.findAndCountAll({
        limit:pageSize,
        offset:offset,
        where:{
            roomStatus:{
                [Op.eq]:'已入住'
            }
        }
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'已入住房间查询成功！',
            roomList:data.rows.map(item =>{
                return item.get()
            }),
            count:data.count
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

roomRouter.get('/getAllRoomPrice',(req,res) =>{//查询所有的房间以及价格
    const { pageSize, offset } = req.query;
    room.findAndCountAll({
        limit:pageSize,
        offset:offset,
        include:[{
            model:roomType,
            foreignKey:'type',
            attributes:['price','url']
        }]
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'房间信息查询成功!',
            roomList:data.rows.map(item =>{
                return item.get()
            }),
            count:data.count
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

roomRouter.get('/getAllNotINRoomPrice',(req,res) =>{//获取所有未入住的房间信息
    const { pageSize, offset } = req.query;
    room.findAndCountAll({
        limit:pageSize,
        offset:offset,
        where:{
            roomStatus:{
                [Op.eq]:'未入住'
            }
        },
        include:[{
            model:roomType,
            foreignKey:'type',
            attributes:['price','url']
        }]
    })
    .then(data =>{
       res.status(200).json({
           success:true,
           msg:'房间信息查询成功！',
           roomList:data.rows.map(item =>{
               return item.get()
           }),
           count:data.count
       })
    })
})

roomRouter.get('/getAllINRoomPrice',(req,res) =>{//获取所有已入住的房间信息
    const { pageSize, offset } = req.query;
    room.findAndCountAll({
        limit:pageSize,
        offset:offset,
        where:{
            roomStatus:{
                [Op.eq]:'已入住'
            }
        },
        include:[{
            model:roomType,
            foreignKey:'type',
            attributes:['price','url']
        }]
    })
    .then(data =>{
       res.status(200).json({
           success:true,
           msg:'房间信息查询成功！',
           roomList:data.rows.map(item =>{
               return item.get()
           }),
           count:data.count
       })
    })
})

roomRouter.get('/getAllRoomTypePrice',(req,res) =>{
    const { pageSize, offset, type } = req.query;
    room.findAndCountAll({
        where:{
           type:{
               [Op.eq]:type
           }
        },
        include:[{
            model:roomType,
            foreignKey:'type',
            attributes:['price','url']
        }]
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'房型查询成功！',
            roomList:data.rows.map(item =>{
                return item.get()
            }),
            count:data.count
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器出错！'
        })
    })
})

roomRouter.get('/getAllStatusRoom',(req,res) =>{//获取所有该状态的房间信息
    const { pageSize, offset, roomStatus } = req.query;
    room.findAndCountAll({
        limit:pageSize,
        offset:offset,
        where:{
            roomStatus:{
                [Op.eq]:roomStatus
            }
        },
        include:[{
            model:roomType,
            foreignKey:'type',
            attributes:['price','url']
        }]
    })
    .then(data =>{
       res.status(200).json({
           success:true,
           msg:'房间信息查询成功！',
           roomList:data.rows.map(item =>{
               return item.get()
           }),
           count:data.count
       })
    })
})


module.exports = roomRouter